<?php

/* 店铺分类 scategory */
class ScategoryModel extends BaseModel
{
    var $table  = 'scategory';
    var $prikey = 'cate_id';
    var $_name  = 'scategory';
    var $expire = 3600;
    var $_relation  =   array(
        // 一个分类有多个子分类
        'has_scategory' => array(
            'model'         => 'scategory',
            'type'          => HAS_MANY,
            'foreign_key'   => 'parent_id',
            'dependent'     => true
        ),
        // 一个分类对应多种语言
        'has_scategorylang' => array(
            'model'         => 'scategorylang',
            'type'          => HAS_MANY,
            'foreign_key'   => 'cate_id',
            'dependent'     => true
        ),
        // 分类和店铺是多对多的关系
        'belongs_to_storeo2o' => array(
            'model'         => 'storeo2o',
            'type'          => HAS_AND_BELONGS_TO_MANY,
            'middle_table'  => 'category_store',
            'foreign_key'   => 'cate_id',
            'reverse'       => 'has_scategory',
        ),
        // 分类和店铺是多对多的关系
        'belongs_to_store' => array(
            'model'         => 'store',
            'type'          => HAS_AND_BELONGS_TO_MANY,
            'middle_table'  => 'category_store',
            'foreign_key'   => 'cate_id',
            'reverse'       => 'has_scategory',
        ),
    );

    var $_autov = array(
        'parent_id'  => array(
        ),
        'sort_order'    => array(
            'filter'    => 'intval',
        ),
    );
    
    function add($data, $compatible = false)
    {
        $this->clear_cache();
        return parent::add($data, $compatible);
    }
    
    function edit($conditions, $edit_data)
    {
        $this->clear_cache();
        return parent::edit($conditions, $edit_data);
    }
    
    function drop($conditions, $fields = '')
    {
        $this->clear_cache();
        return parent::drop($conditions, $fields);
    }

    /**
     * 取得店铺分类列表
     *
     * @param int $parent_id 大于等于0表示取某个店铺分类的下级店铺分类，小于0表示取所有店铺分类
     * @return array
     */
    function get_list($parent_id = -1)
    {
        $result = array();
        if ($parent_id >= 0)
        {
            $result = $this->findAll(array(
                'conditions' => "parent_id = '$parent_id'",
                'include' => array('has_scategorylang' => array( 'index_key' => array('lang', 'cate_id'))),
                'order' => 'sort_order, cate_id',
            ));
        }
        else
        {
            $result = $this->findAll(array(
                'include' => array('has_scategorylang' => array( 'index_key' => array('lang', 'cate_id'))),
                'order' => 'sort_order, cate_id',
            ));
        }
        return $this->remake_cates($result) ;
    }
    
    function get_by_id($cate_id)
    {
        $result = $this->findAll(array(
                'conditions' => "cate_id = '$cate_id'",
                'include' => array('has_scategorylang' => array( 'index_key' => 'lang')),
            ));
        return $this->remake_cates($result);
    }


    /*
     * 判断名称是否唯一
     */
    function unique($cate_name, $parent_id, $cate_id = 0, $lang = 1)
    {
        $conditions = "parent_id = '" . $parent_id . "' AND cate_name = '" . $cate_name . "' AND lang = {$lang}";
        $cate_id && $conditions .= " AND sc.cate_id <> '" . $cate_id . "'";
        $sql = "select 1 from wl_scategory sc left join wl_scategory_lang scl on sc.cate_id=scl.cate_id where {$conditions}";
        $result = $this->getCol($sql);
        return isset($result[0]) ? false : true;
    }

    /**
     * 把某分类及其上级分类加到数组前
     */
    function get_parents(&$parents, $id)
    {
        $data = $this->get(intval($id));
        array_unshift($parents, array('cate_id' => $data['cate_id'], 'cate_name' => $data['cate_name']));
        if ($data['parent_id'] > 0)
        {
            $this->get_parents($parents, $data['parent_id']);
        }
    }

     /**
     * 取得某分类的所有子孙分类id
     */
    function get_descendant($id)
    {
        $ids = array($id);
        $this->_get_descendant($ids, $id);
        return $ids;
    }
    
    function _get_descendant(&$ids, $id)
    {
        $childs = $this->find("parent_id = '$id'");
        foreach ($childs as $child)
        {
            $ids[] = $child['cate_id'];
            $this->_get_descendant($ids, $child['cate_id']);
        }
    }
    
    function remake_cates($cates)
    {
        $result = $cates;
        foreach ($cates as $key => $cate)
        {
            if(isset($cate['scategory_lang']))
            {
                foreach ($cate['scategory_lang'] as $lang => $value)
                {
                    $result[$key]['cate_name'][$value['lang']] = $value['cate_name'];
                    $result[$key]['cate_name_'.$value['lang']] = $value['cate_name'];
                }
                unset($result[$key]['scategory_lang']);
            }
        }
        return $result;
    }
    
    function clear_cache()
    {
        $cache_server = & cache_server();
        $keys = array(
            'storeo2o_scategory',
        );
        foreach ($keys as $key)
        {
            $cache_server->delete($key);
        }
        
        $cache_server->delete($this->_name);
    }
}

?>